﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Glorysoft.EAP.EAPClient.Common;
using Glorysoft.EAP.EAPClient.Model;
using Glorysoft.EAP.EAPClient.View;
using Glorysoft.EAP.EAPClient.ViewModel;

namespace Glorysoft.EAP.EAPClient.View
{
    /// <summary>
    /// LossSN.xaml 的交互逻辑
    /// </summary>
    public partial class LossSN : Window
    {
        public LossSN()
        {

            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();

            List<Core.Entities.UNITLIST> UNITLIST = TrackOutLossViewModel.UnitList;
            //foreach (var item in PIDLIST)
            //{
            //    DataGridBad.ItemsSource = item.PIDNAME;
            //}
            DataGridBad.ItemsSource = null;
            DataGridBad.ItemsSource = UNITLIST;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private string PidName;
        public string PIDNAME
        {
            get { return PidName; }
            set
            {
                PidName = value;
                RaisePropertyChanged("PIDNAME");
            }
        }

        private void RaisePropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        public static List<Core.Entities.LOSSINFOLIST> LossInfoList;
        public static int count = 0;
        public static string code = "";
        //确认
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string MsG = "";
            count = 0;
            for (int i = 0; i < DataGridBad.Items.Count; i++)
            {
                var aa = DataGridBad.ItemContainerGenerator.ContainerFromIndex(i);
                DataGridRow ObjROw = (DataGridRow)aa;
                if (ObjROw != null)
                {
                    FrameworkElement objElement = DataGridBad.Columns[0].GetCellContent(ObjROw);
                    if (objElement != null)
                    {
                        System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
                        if (objChk.IsChecked == true)
                        {
                            MsG += (DataGridBad.Columns[1].GetCellContent(DataGridBad.Items[i]) as TextBlock).Text + ",";
                            count++;
                        }
                    }
                }
            }
            string[] ms = new string[100];
            ms = MsG.Split(",");
            List<Core.Entities.PIDLIST> PIDLIST = new List<Core.Entities.PIDLIST>();
            //if (MsG == "")
            //{
            //    List<Core.Entities.LOSSINFOLIST> lo= TrackOutLoss.LinShiList;
            //    foreach (Core.Entities.LOSSINFOLIST item in lo)
            //    {
            //        PIDLIST = item.PIDLIST;
            //    }

            //}
            //else
            //{
            for (int i = 0; i < ms.Length - 1; i++)
            {
                var losn = new Core.Entities.PIDLIST();
                losn.PIDNAME = ms[i];
                PIDLIST.Add(losn);
            }
            //}
            List<Core.Entities.LOSSINFOLIST> LinShiList = TrackOutLoss.LinShiList;

            foreach (Core.Entities.LOSSINFOLIST item in LinShiList)
            {
                code = item.LOSSCODE;
            }
            LossInfoList = TrackOutLossViewModel.loss;
            foreach (Core.Entities.LOSSINFOLIST item in LossInfoList)
            {
                if (item.LOSSCODE == code)
                {
                    item.PIDLIST = PIDLIST;
                    item.LOSSCOUNT = item.PIDLIST.Count.ToString();
                }
            }
            if (MsG == "")
            {
                this.Close();
                CMessageBox.Show("未选择SN");
                count = 1;
            }
            else
            {
                this.Close();
                CMessageBox.Show("你选择的SN有" + MsG.Substring(0, MsG.Length - 1));
            }
            TrackOutLoss trackOutLoss = new TrackOutLoss();
            trackOutLoss.Show();
        }


    }
}
